//
//  HuUITools.m
//  need
//
//  Created by AcHu on 15/11/5.
//  Copyright (c) 2015年 oc. All rights reserved.
//

#import "HuUITools.h"
#import "AcHuHeader.h"

@implementation HuUITools

+ (CGSize)getTextWidth:(NSString *)text font:(UIFont *)font
{
    CGSize size =  [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil].size;
    return size;
}
+(CGSize)getTextHeight:(NSString *)text font:(UIFont *)font width:(CGFloat)width
{
    CGRect  rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];;
    return rect.size;
}
+(UIImage *)buttonImageFromColor:(UIColor *)color frame:(CGRect)frame
{
    CGRect rect = frame;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

//将字典转化成json数据
+ (NSString*)dataJsonWithDic:(id)paramObj
{
    NSData *data = [NSJSONSerialization dataWithJSONObject:paramObj options:NSJSONWritingPrettyPrinted error:nil];
    NSString *paramStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    return paramStr;
}
/*!
 * @brief 把格式化的JSON格式的字符串转换成字典
 * @param jsonString JSON格式的字符串
 * @return 返回字典
 */
+ (id)dicWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    NSLog(@"%@", jsonString);
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    id dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                             options:NSJSONReadingMutableLeaves
                                               error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}


//创建view
+(UIView *)viewRect:(CGRect)rect color:(UIColor *)color radius:(float)radius fatherView:(UIView *)view
{
    UIView *reView = [[UIView alloc]initWithFrame:rect];
    
    reView.backgroundColor = color == nil ? [UIColor whiteColor] : color;
    if (radius) {
        reView.layer.cornerRadius = radius;
        reView.clipsToBounds = YES;
    }
    [view addSubview:reView];
    return reView;
}

//创建imageView
+(UIImageView *)imageView:(CGRect)rect image:(UIImage *)image radius:(float)radius fatherView:(UIView *)view
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:rect];
    imageView.clipsToBounds = YES;
    if (image) {
        imageView.image = image;
    }else{
        [self label:rect bgColor:[UIColor whiteColor] text:@"未加载图片" font:[UIFont boldSystemFontOfSize:24] textColor:nil textAlignment:NSTextAlignmentCenter fatherView:imageView];
    }
    
    [view addSubview:imageView];
    
    return imageView;
}

//创建textField
+ (UITextField *)textField:(CGRect)rect borderStyle:(UITextBorderStyle)style font:(UIFont *)font textColor:(UIColor *)color place:(NSString *)str keyBordType:(UIKeyboardType)type secure:(BOOL)entry clearButMode:(BOOL)isOrNo clearBegin:(BOOL)foo delegate:(id<UITextFieldDelegate>)se fatherView:(UIView *)view
{
    UITextField *textfield = [[UITextField alloc]initWithFrame:rect];
    textfield.borderStyle = UITextBorderStyleRoundedRect;
    textfield.font = font;
    textfield.textColor = color;
    textfield.placeholder = str;
    textfield.secureTextEntry = entry;
    textfield.keyboardType = type ? type : UIKeyboardTypeDefault;
    textfield.autocorrectionType = UITextAutocorrectionTypeNo;
    textfield.clearButtonMode = isOrNo == YES ? UITextFieldViewModeAlways : UITextFieldViewModeNever;
    textfield.clearsOnBeginEditing = foo;
    textfield.delegate = se;
    [view addSubview:textfield];
    return textfield;
}

//创建button基于图片
+ (UIButton *)butWithImage:(CGRect)rect image:(UIImage *)image bgImage:(UIImage *)bgImage highlightedImage:(UIImage *)hImage radius:(float)radius addTarg:(id)se action:(SEL)sel fatherView:(UIView *)view
{
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    but.frame = rect;
    if (image) {
        [but setImage:image forState:UIControlStateNormal];
    }
    if (bgImage) {
        [but setBackgroundImage:bgImage forState:UIControlStateNormal];
    }
    if (hImage) {
        [but setImage:hImage forState:UIControlStateHighlighted];
    }
    but.layer.cornerRadius = radius;
    but.clipsToBounds = YES;
    [but addTarget:se action:sel forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:but];
    return but;
}

//创建button基于文字
+(UIButton *)butWithTitle:(CGRect)rect title:(NSString *)title font:(UIFont *)font titleColor:(UIColor *)co bgColor:(UIColor *)bgCol radius:(float)radius addTarg:(id)se action:(SEL)sel fatherView:(UIView *)view
{
    UIButton *but = [UIButton buttonWithType:UIButtonTypeSystem];
    but.frame = rect;
    but.backgroundColor = bgCol;
    [but setTitle:title forState:UIControlStateNormal];
    but.titleLabel.font = font;
    [but setTitleColor:co forState:UIControlStateNormal];
    but.layer.cornerRadius = radius;
    but.clipsToBounds = YES;
    [but addTarget:se action:sel forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:but];
    return but;
}

//创建label
+(UILabel *)label:(CGRect)rect bgColor:(UIColor *)bgCol text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textcol textAlignment:(NSTextAlignment)ali fatherView:(UIView *)view
{
    UILabel *label = [[UILabel alloc]initWithFrame:rect];
    label.backgroundColor = bgCol;
    label.text = text;
    label.font = font;
    label.textColor = textcol == nil ? [UIColor blackColor] : textcol;
    label.textAlignment = (void *)ali == nil ? NSTextAlignmentLeft : ali;
    [view addSubview:label];
    return label;
}

//创建textView
+(UITextView *)textView:(CGRect)rect font:(UIFont *)font textColor:(UIColor *)color delegate:(id<UITextViewDelegate>)se fatherView:(UIView *)view
{
    UITextView *textView = [[UITextView alloc]initWithFrame:rect];
    textView.textColor = color;
    textView.textAlignment = NSTextAlignmentLeft;
    textView.delegate = se;
    textView.font = font;
    [view addSubview:textView];
    return textView;
}

//创建scroll
+(UIScrollView *)scrollView:(CGRect)rect bgColor:(UIColor *)col size:(CGSize)size offset:(CGPoint)point cancelXProgress:(BOOL)isOrNo delegate:(id<UIScrollViewDelegate>)se fatherView:(UIView *)view
{
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:rect];
    scroll.backgroundColor = col;
    scroll.contentSize = size;
    scroll.bounces = NO;
    scroll.showsHorizontalScrollIndicator = isOrNo == NO ? NO :YES;
    scroll.showsVerticalScrollIndicator = scroll.showsHorizontalScrollIndicator == YES ? NO :YES;
    scroll.pagingEnabled = YES;
    scroll.delegate = se;
    [view addSubview:scroll];
    return scroll;
}

@end
